import { Component, OnInit } from '@angular/core';
import {TipService} from "../../../services/tip.service";
import {ExtendHttpService} from "../../../services/extend-http.service";
import {Router} from "@angular/router";

@Component({
  selector: 'app-alipay',
  templateUrl: './alipay.component.html',
  styleUrls: ['./alipay.component.css']
})
export class AlipayComponent implements OnInit {
  tipType = 'error';
  account = '';
  isLoading = false;
  constructor(
    private tipService: TipService,
    private extendHttp: ExtendHttpService,
    private router: Router
  ) { }

  ngOnInit() {
    cBack =()=> {
      $('#btn-back').trigger('click');
    }

    this.getInfo();
  }

  getInfo() {
    this.extendHttp.get('/agentApp/withdrawals/getZhifb', {}).subscribe(response => {
      let res = response.json();
      if (res['code'] === '0') {
        this.account = res.zfbNumber;
      } else if (res['code'] == '-2001') {
        this.tipType = 'error';
        this.tipService.change(true, res['message']);
        setTimeout(() => {
          this.router.navigate(['/login']);
        }, 1500);
      } else {
        this.tipType = 'error';
        this.tipService.change(true, res['message']);
      }
    }, error => {
      this.tipType = 'error';
      this.tipService.change(true, '系统繁忙，操作失败！');
    });
  }

  submitData() {
    if (!this.account.trim()) {
      this.tipService.change(true, '请输入支付宝帐号');
      return;
    }

    this.isLoading = !this.isLoading;
    this.extendHttp.post('/agentApp/withdrawals/setZhifb', {zfbNumber: this.account}).subscribe(response => {
      let res = response.json();
      if (res['code'] === '0') {
        this.isLoading = !this.isLoading;
        this.tipType = 'success';
        this.tipService.change(true, res['message']);
        setTimeout(() => {
          this.router.navigateByUrl('/login');
        }, 1500);
      } else if (res['code'] === '-2001') {
        this.tipType = 'error';
        this.tipService.change(true, res['message']);
        setTimeout(() => {
          this.router.navigateByUrl('/login');
        }, 1500);
      } else {
        this.isLoading = !this.isLoading;
        this.tipType = 'error';
        this.tipService.change(true, res['message']);
      }
    }, error => {
      this.isLoading = !this.isLoading;
      this.tipType = 'error';
      this.tipService.change(true, '系统繁忙，操作失败！');
    });
  }

}
